package com.zmx.patientend.view.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.zmx.patientend.BaseActivity;
import com.zmx.patientend.R;
import com.zmx.patientend.adapter.ArchivesDetailsAdapter;
import com.zmx.patientend.fragment.HomeFragment;
import com.zmx.patientend.fragment.patient.CaseManagementFragment;
import com.zmx.patientend.okhttp.OkHttp3ClientManager;
import com.zmx.patientend.pojo.ArchivesDetails;
import com.zmx.patientend.utils.AllUtil;
import com.zmx.patientend.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 时间：2020/6/18 20:35
 * 功能：档案详情
 */
public class ArchivesDetailsActivity extends BaseActivity {

    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.looking_doctor)
    Button lookingDoctor;

    private String pid;

    private ArchivesDetailsAdapter adapter;
    private List<ArchivesDetails> list;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_archives_details;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);
        pid = this.getIntent().getStringExtra("patient_id");

        adapter = new ArchivesDetailsAdapter(mActivity);
        list = new ArrayList<>();
        recycler.setLayoutManager(new LinearLayoutManager(mActivity));
        recycler.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(View view, int listPosition) {

                Intent intent = new Intent(mActivity, CArchivesDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("pomr_id",list.get(listPosition).getId()+"");
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();

        getPomrList();

    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){

                case 1:

                    try {

                        list.clear();
                        JSONObject object = new JSONObject(msg.obj.toString());
                        JSONArray data = object.getJSONArray("data");
                        for (int i=0;i<data.length();i++){

                            JSONObject jsonObject = data.getJSONObject(i);
                            ArchivesDetails ad = new ArchivesDetails();
                            ad.setId(jsonObject.getInt("id"));
                            ad.setAsktime(jsonObject.getString("asktime"));
                            ad.setContent(jsonObject.getString("content"));

                            JSONArray pictures = jsonObject.getJSONArray("picture");
                            List<String> ls = new ArrayList<>();
                            for (int j=0;j<pictures.length();j++){
                                ls.add(pictures.get(j).toString());
                            }
                            ad.setPicture(ls);
                            list.add(ad);
                        }

                        adapter.setListAndNotifyDataSetChanged(list);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

            }
        }
    };

    @OnClick({R.id.upload, R.id.looking_doctor})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.upload:

                Intent intent = new Intent();
                intent.putExtra("patient_id",pid);
                intent.setClass(mActivity, UploadTheCasesActivity.class);
                startActivity(intent);

                break;
            case R.id.looking_doctor:

                Intent intent1 = new Intent();
                intent1.setClass(mActivity, AllDoctorActivity.class);
                startActivity(intent1);

                break;
        }
    }


    public void getPomrList() {

        List<String> ls = new ArrayList<>();
        ls.add(pid);

        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getPomrList");
        params.put("data[patient_id]", pid);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/admin/api.pomr/entry", params, handler, 1, 404);

    }

}
